package com.bnlr.lrparking.appui.paypwd;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bnlr.lrparking.app.Application;
import com.bnlr.lrparking.R;
import com.bnlr.lrparking.base.BaseActivity;
import com.bnlr.lrparking.app.AppConfig;
import com.bnlr.lrparking.event.SetPayPasswordEvent;
import com.bnlr.lrparking.model.BaseArrayBean;
import com.bnlr.lrparking.model.BaseBean;
import com.bnlr.lrparking.constant.LrConstant;
import com.bnlr.lrparking.constant.Urls;
import com.bnlr.lrparking.util.CommonUtil;
import com.bnlr.lrparking.util.pwddialog.SecurityEditCompileListener;
import com.bnlr.lrparking.util.pwddialog.SecurityPasswordEditText;
import com.bnlr.nohttp.CallServer;
import com.bnlr.nohttp.CustomHttpListener;
import com.yolanda.nohttp.NoHttp;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by hp on 2017/8/21.
 * 设置支付密码
 */

public class PayPasswordActivity extends BaseActivity {

    private TextView tvNavTitle;
    private SecurityPasswordEditText setpay_security,setpay_security1;
    private String setpay="",setpay1="";
    private TextView tv_setpaypwd;
    private String phonepwd,tag;
    private String loginPsw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paypassword);
        initView();
    }

    private void initView(){
        Intent intent=getIntent();
        phonepwd=intent.getStringExtra("phonepwd");
        tag=intent.getStringExtra("tag");//reset重置支付密码 set设置支付密码 alter修改
        tvNavTitle=(TextView)findViewById(R.id.tv_nav_title);
        if (tag.equals("set")) {
            tvNavTitle.setText("设置支付密码");
        }else if (tag.equals("alter")){
            tvNavTitle.setText("修改支付密码");
        }else if (tag.equals("reset")){
            tvNavTitle.setText("重置支付密码");
            loginPsw= intent.getStringExtra("mima");
        }
        tv_setpaypwd=(TextView)findViewById(R.id.tv_setpaypwd);
        tv_setpaypwd.setOnClickListener(this);
        setpay_security = (SecurityPasswordEditText)findViewById(R.id.setpay_security);
        setpay_security.getEditText(this);
        setpay_security.setSecurityEditCompileListener(new SecurityEditCompileListener() {
            @Override
            public void onNumCompleted(String num) {
                String[] arr = num.split("");
                boolean isSame = false;
                for (int i = 0; i < 5; i++) {
                    if (arr[i+1].equals(arr[i+2])) {
                        isSame = true;
                    } else {
                        isSame = false;
                        break;
                    }
                }
                if (isSame) {
                    ToastUtils.showShort("密码不能为一样的数字");
                    setpay_security.clearEdit();
                    return;
                }
                boolean isYiyang = false;
                for (int i = 0; i < 5; i++) {
                    if (Integer.parseInt(arr[i+2]) - Integer.parseInt(arr[i+1]) == 1 || Integer.parseInt(arr[i+2]) - Integer.parseInt(arr[i+1]) == -1) {
                        isYiyang = true;
                    } else {
                        isYiyang = false;
                        break;
                    }
                }
                if (isYiyang) {
                    ToastUtils.showShort("密码不能为连续数字");
                    setpay_security.clearEdit();
                    return;
                }
                setpay=num;
            }
        });
        setpay_security1 = (SecurityPasswordEditText)findViewById(R.id.setpay_security1);
        setpay_security1.setSecurityEditCompileListener(new SecurityEditCompileListener() {
            @Override
            public void onNumCompleted(String num) {
                setpay1=num;
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_setpaypwd:
                if (setpay.equals("") || setpay1.equals("")) {
                    ToastUtils.showShort("密码不能为空");
                    return;
                }
                if (!setpay.equals(setpay1)) {
                    ToastUtils.showShort("两次密码不一致");
                    return;
                }
                if (tag.equals("set")) {
                    setPayPwd(setpay1,"设置");//设置支付密码
                }else if (tag.equals("alter")){
                    setPayPwd(setpay1,"修改");//修改支付密码
                }else if (tag.equals("reset")){
                    resetPayPwd(setpay1,"重置");//修改支付密码
                }
                break;
        }
    }

    /**
     * 重置
     * @param newPwd
     * @param content
     */
    private void resetPayPwd(String newPwd, final String content) {
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "ResetPayPassword");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("payPwd", newPwd);
            mRequest.add("loginPwd",loginPsw);
            mRequest.add("idCard", phonepwd);
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<BaseArrayBean>(baseContext, true, BaseArrayBean.class) {
                        @Override
                        public void doWork(BaseArrayBean data, String code) {
                            LogUtils.e(data.toString());
                            if (code.equals("1")) {
                                EventBus.getDefault().post(new SetPayPasswordEvent());
                                ToastUtils.showShort(content+"成功");
                                finish();
                            }
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {
                            if (code.equals("0")){
                                try {
                                    ToastUtils.showShort(""+obj.getString("msg"));
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }

    //设置或修改支付密码
    private void setPayPwd(String newPwd, final String content){
        try {
            mRequest = NoHttp.createStringRequest(Urls.NEW_IP, LrConstant.POST);
            mRequest.add("action", "SetPayPassword");
            mRequest.add("phonePara", Application.DEVICE_ID);
            mRequest.add("uid", AppConfig.getInstance().getString("uid", ""));
            mRequest.add("newPwd", newPwd);
            mRequest.add("oldPwd", phonepwd);
            LogUtils.e(newPwd+"--"+phonepwd);
            CallServer.getRequestInstance().add(baseContext, mRequest,
                    new CustomHttpListener<BaseBean>(baseContext, true, BaseBean.class) {
                        @Override
                        public void doWork(BaseBean data, String code) {
                        }

                        @Override
                        public void onFinally(JSONObject obj, String code, boolean isSucceed) {

                            try {
                                super.onFinally(obj, code, isSucceed);
                                if (code.equals("1")) {
                                    EventBus.getDefault().post(new SetPayPasswordEvent());
                                    AppConfig.getInstance().putString("paypassword", "1");
                                    ToastUtils.showShort(content+"成功");
                                    finish();
                                }else if (code.equals("0")){
                                    CommonUtil.showToask(PayPasswordActivity.this,obj.getString("msg"));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }, true);
        } catch (Exception e) {

        }
    }
}
